package SocialMediaSentiment;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;

public class SortLexicon {
	public static void main( String args[] ){
		File file = new File("CounterTrainSetAll.txt");
		StringBuffer contents = new StringBuffer();
		BufferedReader reader = null;



		Map<String, Integer> woordenlijst = new HashMap<String, Integer>();

		try {
			reader = new BufferedReader(new FileReader(file));
			String text = null;

			// repeat until all lines is read
			while ((text = reader.readLine()) != null) {
				contents.append(text).append(System.getProperty("line.separator"));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (reader != null) {
					reader.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		char[] chararray = new char[contents.length()];
		contents.getChars(0, contents.length(), chararray, 0);
		String wordcount,largestWord = "";
		String[] word;
		int largest=0,count;
		StringTokenizer st = new StringTokenizer(contents.toString(), "\r\n\t\f");
		while(st.hasMoreTokens()){

			wordcount = st.nextToken();
			word = wordcount.split(" ");
			count = Integer.parseInt(word[1]);
			if(count>largest){
				largest = count;
				largestWord = word[0];
			}
			woordenlijst.put(word[0], count);

		}
		Iterator<String> k = woordenlijst.keySet().iterator();
		int lenght = woordenlijst.size(),left;
		try{	
			FileWriter fstream = new FileWriter("sortedLexicon.txt");
			BufferedWriter out = new BufferedWriter(fstream);
			
			while(k.hasNext())
			{
				String currentword;


				while(k.hasNext()){
					currentword = k.next();

					count = woordenlijst.get(currentword);
					if(count>largest){
						largest = count;
						largestWord = currentword;
					}
				}
				/////
				
				StandardFunctions.CorrectWord(largestWord);
				if(StandardFunctions.getInLexicon()){
					out.write(largestWord);
					out.newLine();
				}				
				//System.out.printf("Corrected %s to %s \n",largestWord, correctWord);

				/////
				

				woordenlijst.remove(largestWord);
				
				//reset variables;
				k = woordenlijst.keySet().iterator();
				largestWord = "";
				largest = 0;
				left = woordenlijst.size();

				System.out.println(left+" "+((double)(lenght-left)/lenght)*100);



			}
			out.close();
		}catch (Exception e){//Catch exception if any
			System.err.println("Error: " + e.getMessage());
			System.err.println(e.toString());
			e.printStackTrace();
		}




	}
}
